/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.common.utils.excel.ExportExcel;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.base.entity.CustomerGroup;
import com.chlitina.store.modules.base.entity.CustomerGroupDetail;
import com.chlitina.store.modules.base.entity.CustomerGroupExport;
import com.chlitina.store.modules.base.service.CustomerGroupDetailService;
import com.chlitina.store.modules.base.service.CustomerGroupService;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 客户组管理Controller
 * @author lvbo
 * @version 2013-07-23
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/base/customerGroup")
public class CustomerGroupController extends BaseController {

	@Autowired
	private CustomerGroupService customerGroupService;
	
	@Autowired
	private CustomerGroupDetailService customerGroupDetailService;
	
	@ModelAttribute
	public CustomerGroup get(@RequestParam(required=false) Long id) {
		if (id != null){
			return customerGroupService.get(id);
		}else{
			return new CustomerGroup();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(CustomerGroup customerGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		Store store = StoreUtils.getStore();
		if (store != null){
			customerGroup.setStore(store);
		}
        Page<CustomerGroup> page = customerGroupService.find(new Page<CustomerGroup>(request, response), customerGroup); 
        model.addAttribute("page", page);
		return "modules/base/customerGroupList";
	}

	@RequestMapping(value = "detail")
	public String detail(CustomerGroup customerGroup, Model model) {
		model.addAttribute("customerGroup", customerGroup);
		return "modules/base/customerGroupDetail";
	}
	
	@RequestMapping(value = "form")
	public String form(CustomerGroup customerGroup, Model model) {
		model.addAttribute("customerGroup", customerGroup);
		return "modules/base/customerGroupForm";
	}

	@RequestMapping(value = "save")
	public String save(CustomerGroup customerGroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, customerGroup)){
			return form(customerGroup, model);
		}
		customerGroupService.save(customerGroup);
		addMessage(redirectAttributes, "保存客户群成功");
		return "redirect:"+Global.ADMIN_PATH+"/base/customerGroup/";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		List<CustomerGroupDetail> cgList = customerGroupDetailService.getByGroupId(id);
		for (CustomerGroupDetail cg : cgList) {
			customerGroupDetailService.delete(cg);
		}
		customerGroupService.delete(id);
		addMessage(redirectAttributes, "删除客户组成功");
		return "redirect:"+Global.ADMIN_PATH+"/base/customerGroup/";
	}
	
	@RequestMapping(value = "export")
	public String exportFile(String groupName, HttpServletRequest request,
			HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String fileName = "";
		String errorMsg = "";
		ExportExcel writeFile = null;
		try {
			fileName = "customerGroup" + DateUtils.getDate("yyyyMMddHHmmss") + ".xls";
			List<CustomerGroupExport> employees = customerGroupService.export(groupName);
			if (employees == null || employees.size() == 0) {
				errorMsg = "不存在导出数据";
			} else {
				writeFile = new ExportExcel("客户群数据", CustomerGroupExport.class, 1).setDataList(employees);
				writeFile.write(response, fileName);
				return null;
			}
		} catch (Exception e) {
			errorMsg = "导出客户群数据失败！";
			addMessage(redirectAttributes, errorMsg);
		} finally{
			if (writeFile != null) {
				writeFile.dispose();
			}
		}
		System.out.println(fileName);
		return "redirect:"+Global.ADMIN_PATH+"/base/customerGroup/";
	}
}
